<template>
  <div class="fm">
    <!-- 背景 -->
    <div class="fm__background" :style="{ backgroundImage: 'url(' + songs.al.picUrl + '?param=10y10)' }"></div>
    <!-- 标题 -->
    <div class="fm__header">
      <div class="fm__header__name">{{ songs.name }}</div>
      <div class="fm__header__ar">
        <p>作者：</p>
        <span v-for="item in songs.ar">{{item.name}}</span>
      </div>
      <div class="fm__header__al">
        <p>专辑：</p>
        <span>{{ songs.al.name }}({{songs.alia[0]}})</span>
      </div>
    </div>
    <div class="fm__box">
      <!-- 封面 -->
      <div class="fm__cover">
        <img :src="songs.al.picUrl" :alt="'歌曲ID=' + songs.al.id" />
      </div>
      <!-- 歌词 -->
      <div class="fm__lyric">
        <div class="fm__lyric__content">
          <div class="fm__lyric__content__item" v-for="item in lyric" :key="item.time"
            :class="{active:compareTime(audioCurrentTime,item.time,item.next)}">
            <span>{{ item.lyric }}</span>
          </div>
        </div>
        <!-- 左右歌词角标 -->
        <div class="fm__lyric__arrow">
          <div class="fm__lyric__arrow__left">
            <icon-right-one theme="filled" size="18" :strokeWidth="4" />
          </div>
          <div class="fm__lyric__arrow__right">
            <icon-left-one theme="filled" size="18" :strokeWidth="4" />
          </div>
        </div>
      </div>
    </div>
    <div class="control">
      <icon-unlike theme="filled" size="20" :strokeWidth="3" title='不喜欢' class="play-one unlike"
        @click="playNext('垃圾桶')" />
      <icon-play-one v-show="audioStatus==false" theme="filled" size="25" :strokeWidth="3" title='点击播放' class="play-one"
        @click="play" />
      <icon-pause v-show="audioStatus==true" theme="filled" size="25" :strokeWidth="3" title='点击暂停' class="play-one"
        @click="pause" />
      <icon-go-end theme="filled" size="25" :strokeWidth="3" title='下一首' class="play-one" @click="playNext" />
    </div>
  </div>
</template>

<script setup>
import { storeToRefs } from "pinia"
import { usePlaylistStore } from "@/pinia/module/playlist.js"
import { useDiscoverStore } from "@/pinia/module/discover.js"
import { onMounted, ref, watch } from "vue"
import { debounce } from "@/utils/debounce.js";

const { get_songDetail } = usePlaylistStore()//播放单首歌曲方法
const { audioStatus, songs, lyric, audioCurrentTime, audioDuration } = storeToRefs(usePlaylistStore())

const { get_personalFm } = useDiscoverStore()
const { fm } = storeToRefs(useDiscoverStore())

let fmIndex = ref(0)//私人FM歌曲索引
let lyricIndex = ref(0)//歌词索引

const play = () => {
  get_songDetail(fm.value[fmIndex.value].id)//播放单首歌曲
  audioStatus.value = true
}

const pause = () => {
  audioStatus.value = false
}

// 下一首（添加防抖函数）
const playNext = debounce((value) => {
  if (value) {
    console.log("添加到垃圾桶");
    // TODO: 添加到垃圾桶
  }
  fmIndex.value++
  // 当前播放的歌曲索引等于歌曲列表长度-1时，获取更多歌曲列表
  if (fmIndex.value == fm.value.length - 1) {
    get_personalFm()//获取私人FM
  }
  // 如果是播放状态切歌
  if (audioStatus.value == true) {
    get_songDetail(fm.value[fmIndex.value].id)//播放单首歌曲
  }
}, 300)

const init = async () => {
  await get_personalFm()//获取私人FM
  // play()
  // console.log("私人FM", songs.value, lyric.value);
}
init()
// 比较当前播放时间是否在歌词时间范围内，传入歌词当前时间和歌词播放时间范围(开始时间和结束时间)
const compareTime = (currentTime, lyricTime, lyricNextTime) => {
  if (currentTime >= lyricTime && currentTime < lyricNextTime) {
    return true
  } else {
    return false
  }
}

onMounted(() => {
  // // 让激活的歌词始终在屏幕中间
  const scrollLyric = () => {
    const activeLyric = document.querySelector(".fm__lyric__content__item.active")
    if (activeLyric) {
      activeLyric.scrollIntoView({ block: "center", behavior: "smooth" })
    }
  }
  // 让激活的歌词始终在歌词容器中间
  // const scrollLyric = () => {
  //   const activeLyric = document.querySelector(".fm__lyric__content__item.active")//获取激活的歌词
  //   const lyricContent = document.querySelector(".fm__lyric__content")//歌词滚动容器
  //   if (activeLyric) {
  //     lyricContent.scrollTop = activeLyric.offsetTop - lyricContent.offsetHeight / 2
  //   }
  // }
  watch(audioCurrentTime, (newVal, oldVal) => {
    scrollLyric()
  })
})

</script>

<style scoped lang="less">
.fm {
  width: 100%;
  min-height: 100%;
  height: auto;
  box-sizing: border-box;
  background-color: #fff;
  z-index: 1;
  position: relative;

  // 标题
  &__header {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding-top: 20px;

    &__name {
      font-size: 26px;
      font-weight: bold;
    }

    &__ar,
    &__al {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      margin-top: 4px;
      margin-bottom: 4px;
    }

    &__ar span:not(:last-child)::after {
      content: "/";
      display: inline-block;
      margin: 0 6px;
    }
  }

  // 盒子
  &__box {
    display: flex;
    justify-content: center;
    margin-top: 20px;
    flex-wrap: wrap;
  }

  // 图片
  &__cover {
    display: flex;
    justify-content: center;
    flex: 1;
    margin-bottom: 20px;

    img {
      border-radius: 0.75em;
      width: 250px;
      height: 250px;
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
      -o-object-fit: cover;
      object-fit: cover;
    }


  }

  // 屏幕小于等于500px时隐藏图片
  @media screen and (max-width: 500px) {
    &__cover {
      display: none;
    }
  }

  // 歌词
  &__lyric {
    display: flex;
    justify-content: center;
    flex: 1;
    height: 250px;
    overflow: hidden;
    min-width: 250px;
    margin-bottom: 20px;
    position: relative;

    &__content {
      width: 100%;
      height: 100%;
      overflow-y: auto;
      overflow-x: hidden;
      -webkit-overflow-scrolling: touch;
      -ms-overflow-style: -ms-autohiding-scrollbar;

      &::-webkit-scrollbar {
        display: none; //隐藏滚动条
      }

      &__item {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 16px;
        margin-top: 10px;
        margin-bottom: 10px;
        color: #3e3e3e;
      }
    }

    // 角标
    &__arrow {
      width: 100%;
      position: absolute;
      top: calc(50% - 16px/2);
      display: flex;
      justify-content: space-between;
      color: #00000085;
      display: none; //隐藏角标
    }
  }

  // 背景
  &__background {
    position: absolute;
    height: 100%;
    width: 100%;
    filter: blur(16px) opacity(0.6);
    background-size: cover;
    border-radius: 0.75em;
    z-index: -1;
  }

  // 激活歌词
  .active {
    color: #000;
    font-weight: bold;
  }
}

.control {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  z-index: 1;

  .play-one {
    backdrop-filter: blur(8px);
    border: 1px solid hsla(0, 0%, 100%, 0.08);
    color: #fff;
    background: #ffffff24;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 50px;
    aspect-ratio: 1/1;
    // width: 35px;
    transition: 0.2s;
    cursor: pointer;

    &:hover {
      background: hsla(0, 0%, 100%, 0.44);
    }
  }
}
</style>